﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;

public class EnumInfo<EM>
        where EM : struct
{
    static EnumInfo()
    {
        EnumType = typeof(EM);
        Name = EnumType.Name;

        Descs = new List<string>();
        MemberDescs = new NameValueCollection();

        MemberInts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

        IntMembers = new Dictionary<int, EM>();

        BJUISelectKVS = new List<dynamic>();

        BJUISelectLST = new List<dynamic>();

        var fields = EnumType.GetFields();
        foreach (var field in fields)
        {
            if (field.FieldType.IsAssignableFrom(EnumType))
            {
                var descAttr = field.GetCustomAttributes(typeof(DescriptionAttribute), false)?.FirstOrDefault() as DescriptionAttribute;

                MemberDescs[field.Name] = descAttr?.Description;

                if (descAttr?.Description != null)
                {
                    Descs.Add(descAttr?.Description);
                }

                int value = Convert.ToInt32((EM)Enum.Parse(EnumType, field.Name));

                MemberInts[field.Name] = value;

                IntMembers[value] = (EM)Enum.Parse(EnumType, field.Name);


                var dyn = new ExpandoObject();
                IDictionary<string, object> dict = dyn;
                dict[value.ToString()] = field.Name;
                BJUISelectKVS.Add(dyn);

                var dyn2 = new ExpandoObject();
                IDictionary<string, object> dict2 = dyn2;
                dict2[field.Name] = field.Name;
                BJUISelectLST.Add(dyn2);
            }
        }
    }

    /// <summary>
    /// 当前枚举类型Type
    /// </summary>
    public static Type EnumType { get; private set; }

    /// <summary>
    /// 枚举名称
    /// </summary>
    public static string Name { get; private set; }

    /// <summary>
    /// 枚举成员 与 枚举描述 字典
    /// </summary>
    public static NameValueCollection MemberDescs { get; private set; }

    /// <summary>
    /// 枚举成员  与 枚举Int值 字典(忽略大小写)
    /// </summary>
    public static Dictionary<string, int> MemberInts { get; private set; }

    /// <summary>
    /// 枚举Int值 与 枚举成员 字典
    /// </summary>
    public static Dictionary<int, EM> IntMembers { get; private set; }

    /// <summary>
    /// 所有枚举成员
    /// </summary>
    public static List<string> Members
    {
        get
        {
            return MemberInts.Keys.ToList();
        }
    }

    /// <summary>
    /// 所有枚举成员的值
    /// </summary>
    public static List<int> Vaues
    {
        get
        {
            return MemberInts.Values.ToList();
        }
    }

    /// <summary>
    /// 所有枚举成员的描述
    /// </summary>
    public static List<string> Descs { get; private set; }


    /// <summary>
    /// 字典
    /// </summary>
    public static List<dynamic> BJUISelectKVS { get; private set; }


    /// <summary>
    /// 列表
    /// </summary>
    public static List<dynamic> BJUISelectLST { get; private set; }

}